﻿using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UserLoging.Dtos;
using UserLoging.Models;
using UserLoging.Services;

namespace UserLoging.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        public readonly IUserRepository _userRepository;
        public readonly IMapper _mapper;
        public UsersController(
            IUserRepository userRepository,
            IMapper mapper
            )
        {
            _userRepository = userRepository;
            _mapper = mapper;
        }
        [HttpOptions]
        public IActionResult Options()
        {
            return Ok();
        }
        [HttpGet]
        [Authorize]
        public async Task<ReturnJson> Users([FromQuery] GetFindUsersinformationDto getFindUsersinformationDto)
        {
            UserIncludeTotapage userIncludeTotapage = await _userRepository.GetUserByFindUsersinformationDtoAsync(getFindUsersinformationDto);
            SetFindUsersinformationDto setFindUsersinformationDto = new SetFindUsersinformationDto()
            {
                Pagenum = getFindUsersinformationDto.Pagenum,
                Totalpage = userIncludeTotapage.Totapage,
                userForFinduserDtos = _mapper.Map<IEnumerable<UserForFinduserDto>>(userIncludeTotapage.Users)
            };
            return new ReturnJson
            {
                Data = setFindUsersinformationDto,
                Status = 200,
                Message = "请求成功"
            };
        }
        [HttpOptions("{userId}")]
        public IActionResult EditUserByUserIdOptions() 
        {
            return Ok();
        }
        [HttpPut("{userId}")]
        [Authorize(Roles ="admin")]
        public async Task<ReturnJson> EditUserByUserId([FromRoute]Guid userId,[FromBody] EditUserFindDto editUserFindDto) 
        {
            if (!(await _userRepository.UserExistsAsync(userId)))
            {
                return new ReturnJson
                {
                    Data = null,
                    Message = "用户不存在",
                    Status = 404
                };
            }
            User user = await _userRepository.GetUserByUserIdAsync(userId);
           user= _userRepository.EditUser(user, editUserFindDto);
            if (!(await _userRepository.SaveAsync())) 
            {
                return new ReturnJson
                {
                    Data = null,
                    Message = "保存失败",
                    Status = 404
                };
            }
            return new ReturnJson
            {
                Data = user,
                Message = "修改成功 ",
                Status = 200
            };

        }
        [HttpDelete("{userId}")]
        [Authorize(Roles = "admin")]
        public async Task<ReturnJson> DeleteUserByUserId([FromRoute] Guid userId) 
        {
            if (!(await _userRepository.UserExistsAsync(userId))) 
            {
                return new ReturnJson
                {
                    Data = null,
                    Message = "用户不存在",
                    Status = 404
                };
            };
            User user = await _userRepository.GetUserByUserIdAsync(userId);
            _userRepository.DeleteUser(user);
            if (!(await _userRepository.SaveAsync())) 
            {
                return new ReturnJson
                {
                    Data = null,
                    Message = "删除失败",
                    Status = 404
                };
            }
            return new ReturnJson
            {
                Data = null,
                Message = "删除成功",
                Status = 200
            };
        }
        [HttpOptions("Roles/{userId}")]
        public IActionResult EditUserRolesByUserIdOptions()
        {
            return Ok();
        }
      
    }
}
